package momo.app.status;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import momo.app.comms.CommsManager;
import momo.app.structure.PeerList;


public class PlanetLabPeriodicalCheck {
	
	private Log log = LogFactory.getLog(PlanetLabPeriodicalCheck.class);
	private int checkTime = 0; //in the aspect of seconds, default = 30sec.
	private StatusManager sm = null;
	
	public PlanetLabPeriodicalCheck(int checkTime) {
		this.checkTime = checkTime;
	}
	
	public PlanetLabPeriodicalCheck() {
		this.checkTime = 2 * 60;
	}
	
	public void run() {
		Thread t = new Thread() {
			public void run() {
				try {
					while ( true ) {
						try {
							sleep( checkTime * 1000 );
							
							sm = StatusManager.isStatusManagerCreated()?StatusManager.getInstance():null;
							
							if ( sm.isRoot() )
								CommsManager.getInstance().startRestructure();
							
						}catch ( InterruptedException ie ) {
							if ( log.isErrorEnabled() )
								log.error(ie);
						}
					}
				}catch (Exception e) {
					if ( log.isErrorEnabled() )
						log.error(e);
				}
			}
		};
		t.start();
	}
	
}//end of class PlanetLabPeriodicalCheck
